using System;
using System.Collections.Generic;
using System.Text;

namespace Ant1
{
    class GameManager
    {
        public string DebugInfo;
        public int BoardX=100;
        public int BoardY=100;
        public int LifeTickLimit = 100;

        private List<GameObject> objects;

        public GameManager()
        {
            objects = new List<GameObject>();
        }

        public void AddObject(GameObject obj)
        {
            objects.Add(obj);
        }

        /// <summary>
        /// Process on tick in the game
        /// </summary>
        public void Tick()
        {

            //Send each object a tick (Mostly debug)
            foreach (GameObject obj in objects)
            {
                obj.OnTick();
                obj.TickCount++;
            }

            
            

            #region Process OnHitWall
            foreach (GameObject obj in objects)
            {
                if (obj.X < 0)
                {
                    obj.X = 0;
                    obj.OnHitWall();
                }
                if (obj.Y < 0)
                {
                    obj.Y = 0;
                    obj.OnHitWall();
                }


                if (obj.X > BoardX)
                {
                    obj.X = BoardX;
                    obj.OnHitWall();
                }
                if (obj.Y > BoardY)
                {
                    obj.Y = BoardY;
                    obj.OnHitWall();
                }
            }
            #endregion

            //Proccess Senses
            foreach (GameObject obj in objects)
                foreach(Senses.SensesObject So in obj.Senses)
                    if(So.TestSense(obj))
                        obj.OnSense(So);


            //Process Desictions
            foreach (GameObject obj in objects)
                if (obj.currentDecision != null)
                    if(obj.currentDecision.OnTick(obj))
                        obj.OnDoneDecision();

            //Debug Stuff
            DebugInfo = String.Empty;
            foreach (GameObject obj in objects)
            {
                DebugInfo += obj.GetThough();
            }


            //Kill old guys
            for (int x = objects.Count - 1; x >= 0; x--)
                if (objects[x].TickCount > LifeTickLimit)
                    objects.RemoveAt(x);

            //Make sure we have some guys in the world

        }


    }
}
